package com.og.validator;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.og.backingbean.Company;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
@FacesValidator("companyValidator")
public class CompanyValidator implements Validator, Serializable {

	private static final long serialVersionUID = 1L;

	// Log4j	 
	private static final Logger	log	= Logger.getLogger(CompanyValidator.class);
		
	/* Dependency injection */	
    // BackingBean
	@Inject
    private Company company;
	
	/* -------------------- */
		
	/**
	 * Default constructor
	 */
	public CompanyValidator() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
	
		company = (Company) value;
		log.debug("Validate value = " + company);
		if(company == null) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Company must not be null", null);
			throw new ValidatorException(message);
		}
	}
}
